﻿using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Security.Cryptography;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace KP_CommonLibrary.KP_Functions
{
    public class Api_Func
    {
        private const int SC_CLOSE = 61536;
        private const int MF_BYCOMMAND = 0;

        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern int GetSystemMenu(IntPtr hwnd, int bRevert);
        [System.Runtime.InteropServices.DllImport("user32")]

        public static extern int RemoveMenu(int hMenu, int nPosition, int wFlags);
        
        #region RemoveCloseButton
        public static int RemoveCloseButton(IntPtr iHWND)
        {
            int iSysMenu;
            iSysMenu = GetSystemMenu(iHWND, 0);
            return RemoveMenu(iSysMenu, SC_CLOSE, MF_BYCOMMAND);
        }
        #endregion
    }
    public class Encryption
    {
        #region EnCrypt
        public static string EnCrypt(string toEncrypt, string key, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        #endregion

        #region DeCrypt
        public static string DeCrypt(string toDecrypt, string key, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return UTF8Encoding.UTF8.GetString(resultArray);
        }
        #endregion
    }

    #region Class KP_Config
    public class KP_Config
    {
        public static KP_Config KP_Variable;
        
        static KP_Config()
        {
            KP_Variable = new KP_Config();
        }
        
        private Dictionary<string, string> kp_GlobalConfig;

        public Dictionary<string, string> GlobalConfig
        {
            get
            {
                return kp_GlobalConfig;
            }
            set
            {
                kp_GlobalConfig = value;
            }
        }

        public string this[string configKey]
        {
            get
            {
                if (kp_GlobalConfig.ContainsKey(configKey))
                    return kp_GlobalConfig[configKey];
                else return string.Empty;
            }
            set
            {
                if (kp_GlobalConfig.ContainsKey(configKey))
                    kp_GlobalConfig[configKey] = value;
                else kp_GlobalConfig.Add(configKey, value);
            }
        }
        public static int Get_Int32(string keyName)
        {
            return Convert.ToInt32(KP_Variable[keyName]);
        }
    }
    #endregion
}
